package ecom.appcode.web.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ecom.appcode.business.OrderDetailLogic;
import ecom.appcode.business.OrderDetailLogicImpl;
import ecom.appcode.business.OrderLogic;
import ecom.appcode.business.OrderLogicImpl;
import ecom.appcode.common.bean.OrderBean;
import ecom.appcode.common.daobean.OrderDaoBean;
import ecom.appcode.common.daobean.OrderDetailDaoBean;

/**
 * Servlet implementation class OrderManagerAction
 */
public class OrderManagerAction extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrderManagerAction() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String action = request.getParameter("btAction");
        if ("ViewAllOrderDetail".equals(action)) {
            OrderDetailLogic orderDetailLogic = new OrderDetailLogicImpl();
            List<OrderBean> detailList = orderDetailLogic.loadOrderAndDetail();

            HttpSession session = request.getSession();
            session.setAttribute("ORDER_DETAIL_LIST", detailList);
            RequestDispatcher rd = request.getRequestDispatcher("order-management.jsp");
            rd.forward(request, response);
        } else if("DeleteOrderDetail".equals(action)) {
            int id = Integer.parseInt(request.getParameter("txtID"));
            OrderDetailLogic orderDetailLogic = new OrderDetailLogicImpl();
            boolean result = orderDetailLogic.deleteOrderDetail(id);
            if(result) {
                List<OrderBean> detailList = orderDetailLogic.loadOrderAndDetail();
                HttpSession session = request.getSession();
                session.setAttribute("ORDER_DETAIL_LIST", detailList);
                RequestDispatcher rd = request.getRequestDispatcher("order-management.jsp");
                rd.forward(request, response);
            }
        } else if("UpdateOrder".equals(action)) {
            int orderID = Integer.parseInt(request.getParameter("txtOrderID"));
            String string_status = request.getParameter("txtStatus");
            boolean status = false;
            if("FALSE".equals(string_status)) {
                status = true;
            }
            OrderLogic order = new OrderLogicImpl();
            boolean result = order.updateOrderStatus(status, orderID);
            if(result) {
                OrderDetailLogic orderDetailLogic = new OrderDetailLogicImpl();
                List<OrderBean> detailList = orderDetailLogic.loadOrderAndDetail();

                HttpSession session = request.getSession();
                session.setAttribute("ORDER_DETAIL_LIST", detailList);
                RequestDispatcher rd = request.getRequestDispatcher("order-management.jsp");
                rd.forward(request, response);
            }
            
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // TODO Auto-generated method stub
    }

}
